from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import re
import json
from order.models import *
from user.models import *
from .models import *


# Create your views here.
@csrf_exempt
def add_room(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        user = User.objects.get(username=username)
        if user.admin:
            room = Room()
            room.type = data_json.get('type', 0)
            room.name = data_json.get('name', "")
            room.size = data_json.get('size', 0)
            room.price = data_json.get('price', 0)
            room.city = data_json.get('city', "")
            room.address = data_json.get('address', "")
            room.desc = data_json.get('desc', "")
            
            room.mobile = data_json.get('mobile', "")
            room.managerName = data_json.get('managerName', "")
            room.email = data_json.get('email', "")
            room.save()
            return JsonResponse({'errno': 0, 'msg': "添加成功", 'rid': room.id})
        else:
            return JsonResponse({'errno': 1003, 'msg': "非管理员用户"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def remove_room(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        user = User.objects.get(username=username)
        if user.admin:
            rid=data_json.get('rid', 0)
            if Room.objects.filter(id=rid).exists():
                room = Room.objects.get(id=rid)
                room.status = -1
                room.save()
                return JsonResponse({'errno': 0, 'msg': "下架成功"})
            else:
                return JsonResponse({'errno': 1004, 'msg': "房间不存在"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "非管理员用户"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})
    
    
@csrf_exempt
def readd_room(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        user = User.objects.get(username=username)
        if user.admin:
            rid=data_json.get('rid', 0)
            if Room.objects.filter(id=rid).exists():
                room = Room.objects.get(id=rid)
                room.status = 0
                room.save()
                return JsonResponse({'errno': 0, 'msg': "恢复成功"})
            else:
                return JsonResponse({'errno': 1004, 'msg': "房间不存在"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "非管理员用户"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def modify_room(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        user = User.objects.get(username=username)
        if user.admin:
            rid = data_json.get('rid', 0)
            if Room.objects.filter(id=rid).exists():
                room = Room.objects.get(id=rid)
                room.type = data_json.get('type', 0)
                room.name = data_json.get('name', "")
                room.size = data_json.get('size', 0)
                room.price = data_json.get('price', 0)
                room.city = data_json.get('city', "")
                room.address = data_json.get('address', "")
                room.mobile = data_json.get('mobile', "")
                room.save()
                return JsonResponse({'errno': 0, 'msg': "修改成功"})
            else:
                return JsonResponse({'errno': 1004, 'msg': "房间不存在"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "非管理员用户"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def search_rooms(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        else:
            res = []
            for room in Room.objects.all():
                res.append(room.get_info())
            return JsonResponse({'errno': 0, 'msg': "查询成功", 'content': res})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def lookup_room(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
                return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        else:
            rid = data_json.get('rid', 0)
            if Room.objects.filter(id=rid).exists():
                room = Room.objects.get(id=rid)
                return JsonResponse({'errno': 0, 'msg': "查询成功", 'content': room.get_info()})
            else:
                return JsonResponse({'errno': 1003, 'msg': "房间不存在"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})
